#include<bits/stdc++.h>
#define M 200005
using namespace std;
int n, a[M];
long long dp[M * 5];
void solve()
{
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
    memset(dp, -0x3f, sizeof(dp));
    dp[0] = 0;
    long long maxn = 0, tg = 0;
    for(int i = 0; i < n; i++)
    {
        int j = a[i + 1], now = (a[i] == a[i + 1]) * a[i + 1];
        dp[a[i]] = max(dp[a[i]], maxn);
        dp[a[i]] = max(dp[a[i]], dp[j] + (a[i + 1] - now));
        tg += now;
        maxn = max(maxn, dp[a[i]]);
    }
    printf("%lld\n", maxn + tg);
}
signed main()
{
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);
    int T;
    scanf("%d", &T);
    while(T--) solve();
    return 0;
}
